﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Workbench.Presentation
{
    /// <summary>
    /// Interaction logic for POSTypesTreeView.xaml
    /// </summary>
    public partial class POSTypesTreeView : UserControl
    {
        public POSTypesTreeView()
        {
            InitializeComponent();
        }

        private POSTypesTreeViewModel viewModel;

        private void TreeView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);

            if (treeViewItem != null)
            {
                treeViewItem.IsSelected = true;
            }
            else
            {
                if (viewModel.SelectedNode != null)
                {
                    viewModel.SelectedNode.IsSelected = false;
                }
            }
        }

        static TreeViewItem VisualUpwardSearch(DependencyObject source)
        {
            while (source != null && !(source is TreeViewItem))
                source = VisualTreeHelper.GetParent(source);

            return source as TreeViewItem;
        }

        private void Addsubtype_Click(object sender, RoutedEventArgs e)
        {
            var result = PromptDialog.Prompt(this.GetWindow(), "Enter the new subtype", "New Subtype");
            if (!string.IsNullOrWhiteSpace(result))
            {
                viewModel.AddCommand.Execute(result);
            }
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            viewModel = e.NewValue as POSTypesTreeViewModel;
        }

        private void AddPOSType_Click(object sender, RoutedEventArgs e)
        {
            var result = PromptDialog.Prompt(this.GetWindow(), "Enter the new Part-of-speech type", "New Part-of-speech type");
            if (!string.IsNullOrWhiteSpace(result))
            {
                viewModel.AddCommand.Execute(result);
            } 
        }
    }
}
